#include "iot_server.h"


void dev_op(int dev, int on)
{
	if (dev == 1) {
		if (on == 1)
			log_i("led2 green on");
		else if (on == 0)
			log_i("led2 green off");
	}
	if (dev == 2) {
		if (on == 1)
			log_i("led2 orange on\n");
		else if (on == 0)
			log_i("led2 orange off\n");
	}


	

}


//从zigbee命令队列出队节点并发送给zigbee模块终端线程
void * out_cmd_thread(void *arg)
{
	int cnt = 0;
	int dev = -1, on = -1;
	char cmdstr[50]; //保存发给zigbee终端的原始命令字符串
	char devname[30];
	char op[30];
	while (1) {
		pthread_mutex_lock(&cmd_queue_mutex);
		pthread_cond_wait(&cmd_queue_cond, &cmd_queue_mutex); //等待接收命令节点唤醒
		memset(cmdstr, 0, sizeof(cmdstr));
		if(linkqueue_cmdstr_out(pcmdque, cmdstr)) {
			log_d("********************************");
			cnt++;
			log_d("$$$命令链表队列节点%d 出队命令字符串:$$$ ", cnt);
			log_d("%s\n", cmdstr);
			log_d("********************************");
		}
		pthread_mutex_unlock(&cmd_queue_mutex);
		dev = -1;
		on = -1;
		sscanf(cmdstr, "%s%s", devname, op);
		log_d("[%s] [%s]\n", devname, op);
		if (strncmp(devname, "led2_green", strlen(devname)) == 0) dev = 1;
		if (strncmp(devname, "led2_orange", strlen(devname)) == 0) dev = 2;
		if (strncmp(op, "on", strlen(op)) == 0) on = 1;
		if (strncmp(op, "off", strlen(op)) == 0) on = 0;
		dev_op(dev, on); //控制设备
		log_e("out_cmds_thread uname=%s", uname);
		do_cmds(cmdstr, db); //写入数据库
		
	}
			
	return NULL;

}

